#include "window_attach.h"

WindowAttach::WindowAttach() {}

void WindowAttach::SetWindow(HWND child,HWND parent){
    parent_id = parent;
    child_id = child;
    Attach();
    ResetPos();
}

void WindowAttach::ResetPos(){
    if (!IsWindow(parent_id) || !IsWindow(child_id))
        return;
    int dx = GetSystemMetrics(SM_CXEDGE);
    int dy = GetSystemMetrics(SM_CYEDGE);
    // 3. 把窗口挪到父客户区左上角并填满
    RECT rc;
    GetClientRect(parent_id, &rc);
    SetWindowPos(child_id, HWND_TOP,
                 -dx, -dy, rc.right+2*dx, rc.bottom+2*dy,
                 SWP_SHOWWINDOW | SWP_FRAMECHANGED);
}

void WindowAttach::Attach(){
    if (!IsWindow(parent_id) || !IsWindow(child_id))
        return;

    // 1. 先挂父子关系
    SetParent(child_id, parent_id);

    // 1. 彻底去边框
    LONG_PTR style = GetWindowLongPtr(child_id, GWL_STYLE);
    style &= ~(WS_BORDER | WS_DLGFRAME | WS_THICKFRAME | WS_CAPTION);
    style |= WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
    SetWindowLongPtr(child_id, GWL_STYLE, style);

    // 2. 把扩展边框也去干净
    LONG_PTR exStyle = GetWindowLongPtr(child_id, GWL_EXSTYLE);
    exStyle &= ~(WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE | WS_EX_STATICEDGE);
    SetWindowLongPtr(child_id, GWL_EXSTYLE, exStyle);
}